#include "Element.h"
#include "Constants.h"

void getElemChildren(){
   ;
}

void initElement(Element* elem,int nameSize,int docNameSize){
	elem->name = (char*) malloc(nameSize);
	elem->documentName = (char*) malloc(docNameSize);
	vectorListCreate(&(elem->elems));
	vectorListCreate(&(elem->attrs));
}

void releaseElement(Element* elem){
	free(elem->name);
	free(elem->documentName);
	vectorListFree(&(elem->elems));
	vectorListFree(&(elem->attrs));
}

int hydrate(char* str){
	return OPERATION_SUCCESS; 
}

int serialize(char* str){
	return OPERATION_SUCCESS;
}

void printElements(Element* elem){
	
	Element auxElem;
	int i=0;

 	while (vectorListGetItem(elem->elems,i,&auxElem) != -1){
		printElements(&auxElem);
		i++;
	}

	printf("Elem: %s\n",elem->name);
	printAttributes(elem);

}

void printAttributes(Element* elem){
	
	Attr auxAttr;
	int i=0;

 	while (vectorListGetItem(elem->attrs,i,&auxAttr) != -1){
		printf("Atributo %s de value %s\n",auxAttr.name,auxAttr.value);
		i++;
	}


}

int getAttrValue(Element* elem,char* attName,char** value){
	
	Attr auxAttr;
	int i=0;

 	while (vectorListGetItem(elem->attrs,i,&auxAttr) != -1){
		if (strcmp(auxAttr.name,attName) == 0){
			*value = auxAttr.value;
			return 1;
		}
		i++;
	};
	return 0;

}

int getElemId(Element* elem,char** value){
	
	return getAttrValue(elem,"id",value);

}


int getChildElement(Element* srcElem, char* elemName, Element* auxElem){
	

	int i=0;

 	while (vectorListGetItem(srcElem->elems,i,auxElem) != -1){
		if (strcmp(auxElem->name,elemName) == 0){
			return 1;
		}
		i++;
	};
	return 0;


}